package com.example.vo.Fragment

import android.app.AlertDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.vo.Controller.Vo
import com.example.vo.Data.DB_History
import com.example.vo.Data.History
import com.example.vo.R

class HistoryFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val context = this.requireContext()
        val view = inflater.inflate(R.layout.fragment_history, container, false)
        val history_delete: ImageView = view.findViewById(R.id.history_delete)
//
//        val history = gethistory()
//        val history_t:TextView = view.findViewById(R.id.history_t)
//        val history_time:TextView = view.findViewById(R.id.history_time)
//        val result_n:TextView = view.findViewById(R.id.result_n)
//        history_t.text = history[0].item
//        history_time.text = history[0].time
//        result_n.text = history[0].result

        history_delete.setOnClickListener {
            AlertDialog.Builder(context).apply {
                setTitle("是否删除记录")
                setCancelable(false)
                setPositiveButton("确认") { _, _ ->
//                    deletehistory()
                    Toast.makeText(context, "已删除", Toast.LENGTH_SHORT).show()
                }
                setNegativeButton("取消") { _, _ ->
                }
                show()
            }
        }
        return view
    }

    private fun gethistory(): ArrayList<History> {
        val historylist = ArrayList<History>()
        val dbhelper3 = DB_History(Vo.context, "History.db", 1)
        val db = dbhelper3.writableDatabase
        val cursor = db.rawQuery("select * from history limit 1", null)
        if (cursor.moveToFirst()) {
            do {
                val item = cursor.getString(cursor.getColumnIndex("item"))
                val result = cursor.getString(cursor.getColumnIndex("result"))
                val time = cursor.getString(cursor.getColumnIndex("time"))
                historylist.add(History(item, result, time))
            } while (cursor.moveToNext())
        }
        cursor.close()
        return historylist
    }

    private fun deletehistory() {
        val dbhelper4 = DB_History(Vo.context, "History.db", 1)
        val db = dbhelper4.writableDatabase
        db.delete("history", null, null)
    }
}